<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <meta charset="UTF-8">
    <title>用户管理 - SimpleHire</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css" rel="stylesheet">
    <style>
        /* 统一的CSS变量 */
        :root {
            --primary-color: #5A67D8;
            --primary-hover: #434B9A;
            --text-color: #2D3748;
            --light-gray: #F7FAFC;
            --border-color: #E2E8F0;
            --danger-color: #E53E3E;
            --danger-hover: #C53030;
        }

        body {
            font-family: 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
            background-color: var(--light-gray);
            min-height: 100vh;
            color: var(--text-color);
        }

        .admin-header {
            background-color: var(--text-color); /* 深色头部 */
            color: white;
            padding: 1.5rem 0;
            margin-bottom: 2rem;
        }

        .admin-header h1 {
            font-size: 1.5rem;
            margin: 0;
            font-weight: 600;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 1rem;
        }

        .header-buttons a {
            color: white;
            border: 1px solid rgba(255, 255, 255, 0.5);
            border-radius: 8px;
            transition: background-color 0.3s ease, border-color 0.3s ease;
            padding: 0.5rem 1rem;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }

        .header-buttons a:hover {
            background-color: rgba(255, 255, 255, 0.1);
            border-color: white;
        }

        .table-container {
            background-color: white;
            padding: 2rem;
            border-radius: 12px;
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.08);
            overflow-x: auto;
        }

        .table {
            margin: 0;
        }

        .table thead th {
            border-bottom: 2px solid var(--border-color);
            padding-bottom: 1rem;
            font-weight: 600;
        }

        .table tbody tr {
            transition: background-color 0.3s ease;
        }
        .table tbody tr:nth-of-type(odd) {
            background-color: var(--light-gray);
        }
        .table tbody tr:hover {
            background-color: rgba(90, 103, 216, 0.05);
        }

        .btn-action {
            padding: 0.25rem 0.5rem;
            border-radius: 6px;
            font-size: 0.875rem;
            display: inline-flex;
            align-items: center;
            gap: 4px;
        }
        .btn-edit {
            background-color: var(--primary-color);
            color: white;
            border: none;
            transition: background-color 0.3s ease;
        }
        .btn-edit:hover {
            background-color: var(--primary-hover);
        }
        .btn-delete {
            background-color: var(--danger-color);
            color: white;
            border: none;
            transition: background-color 0.3s ease;
        }
        .btn-delete:hover {
            background-color: var(--danger-hover);
        }

        .modal-content {
            border-radius: 12px;
        }
        .modal-header {
            border-bottom: none;
            padding: 1.5rem;
        }
        .modal-footer {
            border-top: none;
            padding: 1.5rem;
        }

        .form-control {
            border-radius: 8px;
            padding: 0.75rem 1rem;
            border: 1px solid var(--border-color);
            transition: border-color 0.3s ease, box-shadow 0.3s ease;
        }
        .form-control:focus {
            outline: none;
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(90, 103, 216, 0.2);
        }

    </style>
</head>
<body>
<div class="admin-header">
    <div class="container d-flex justify-content-between align-items-center">
        <h1><i class="bi bi-people-fill"></i> 用户管理</h1>
        <div class="header-buttons">
            <a th:href="@{/dashboard}"><i class="bi bi-speedometer2"></i> 返回 </a>
            <form th:action="@{/logout}" method="post" class="d-inline">
                <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
                <button type="submit" class="header-buttons" style="background: none; border: none; color: white;">
                    <i class="bi bi-box-arrow-right"></i> 登出
                </button>
            </form>
        </div>
    </div>
</div>

<div class="container">
    <div class="table-container">
        <h3 class="mb-4">用户列表</h3>

        <div th:if="${param.success}" class="alert alert-success">
            操作成功！
        </div>

        <table class="table table-borderless table-hover">
            <thead>
            <tr>
                <th>ID</th>
                <th>用户名</th>
                <th>角色</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <tr th:each="user : ${users}">
                <td th:text="${user.id}"></td>
                <td th:text="${user.username}"></td>
                <td>
                    <span class="badge bg-primary" th:if="${user.role.name() == 'ADMIN'}">管理员</span>
                    <span class="badge bg-secondary" th:if="${user.role.name() == 'USER'}">普通用户</span>
                </td>
                <td>
                    <button class="btn btn-action btn-edit me-2" data-bs-toggle="modal" data-bs-target="#editModal"
                            th:data-user-id="${user.id}"
                            th:data-user-username="${user.username}"
                            th:data-user-role="${user.role.name()}">
                        <i class="bi bi-pencil-square"></i> 编辑
                    </button>
                    <a th:href="@{'/admin/users/' + ${user.id} + '/permissions'}" class="btn btn-action btn-secondary me-2">
                        <i class="bi bi-key-fill"></i> 权限
                    </a>
                    <form th:action="@{'/admin/users/' + ${user.id} + '/delete'}" method="post" class="d-inline">
                        <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
                        <button type="submit" class="btn btn-action btn-delete" onclick="return confirm('确定要删除这个用户吗？');">
                            <i class="bi bi-trash-fill"></i> 删除
                        </button>
                    </form>
                </td>
            </tr>
            </tbody>
        </table>
    </div>
</div>

<div class="modal fade" id="editModal" tabindex="-1" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <form id="editUserForm" method="post">
                <div class="modal-header">
                    <h5 class="modal-title" id="editModalLabel">编辑用户信息</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <input type="hidden" id="editUserId" name="id">
                    <div class="mb-3">
                        <label for="editUsername" class="form-label">用户名</label>
                        <input type="text" class="form-control" id="editUsername" name="username" readonly>
                    </div>
                    <div class="mb-3">
                        <label for="editRole" class="form-label">角色</label>
                        <select class="form-select" id="editRole" name="role" required>
                            <option value="USER">普通用户</option>
                            <option value="ADMIN">管理员</option>
                        </select>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary">保存</button>
                </div>
                <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
            </form>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
    // 编辑模态框数据处理
    const editModal = document.getElementById('editModal');
    editModal.addEventListener('show.bs.modal', function (event) {
        const button = event.relatedTarget;
        const userId = button.getAttribute('data-user-id');
        const username = button.getAttribute('data-user-username');
        const role = button.getAttribute('data-user-role');

        const modalForm = editModal.querySelector('form');
        modalForm.action = '/admin/users/' + userId;

        document.getElementById('editUserId').value = userId;
        document.getElementById('editUsername').value = username;
        document.getElementById('editRole').value = role;
    });
</script>
</body>
</html>